/*
 * 发布者
 * */
function Publisher(){
	//订阅者列表
	this.subscribers=[];
}
//投送方法
Publisher.prototype.deliver=function(data){
	this.subscribers.forEach(
		function(fn){
			fn(data);
		}
	);
	return this;
};

//订阅方法
Function.prototype.subscribe=function(publisher){
	var that=this;
	//some--回调函数至少有一次有返回TRUE则返回TRUE否则返回FALSE
	var alreadyExists=publisher.subscribers.some(
		function(el){
			return el===that;
		}
	);
	if(!alreadyExists){
		publisher.subscribers.push(this);
	}
	return this;
};
//	退订方法
Function.prototype.unsubscribe=function(publisher){
	var that=this;
	publisher.subscribers=fublisher.subscribers.filter(
		function(el){
			return el !== that;
		}
	);
	return this;
};

var Animation=function(o){
	this.onStart=new Publisher,
	this.onComplete=new Publisher,
	this.onTween=new Publisher;
};
Animation.prototype.fly=function(){
	this.onStart.deliver("abc");
	for(var i=0;i<50;i++){
		this.onTween.deliver(i);
	}
	this.onComplete.deliver();
};
var Superman=new Animation();


var putOnCape=function(i){
	console.log(i);
};
var takeOffCape=function(i){
	console.log("超人回来了！");
};
var testSuperFly=function(i){
	console.log("超人飞行数据！"+i);
};
putOnCape.subscribe(Superman.onStart);
takeOffCape.subscribe(Superman.onComplete);
testSuperFly.subscribe(Superman.onTween);
Superman.fly();


